package com.cloud.wms.ui.returned.register;

import android.annotation.SuppressLint;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.ui.returned.contact.ReturnRegisterContact;
import com.cloud.wms.ui.returned.entity.ExpressDeliveryEntity;
import com.cloud.wms.ui.returned.presenter.ReturnRegisterPresenter;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by FC on 2021/5/13.
 * Desc：退货登记选择快递
 */
public class ReturnExpressDeliveryActivity extends BaseBarListActivity<ExpressDeliveryEntity, ReturnRegisterPresenter> implements ReturnRegisterContact.View {
    @BindView(R.id.et_name)
    EditText etName;
    private List<ExpressDeliveryEntity> deliveryEntityList;

    @Override
    protected String barTitle() {
        return "退货登记";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_return_express_delivery;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_return_express_delivery;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mPresenter.getExpressDeliveryMsg();
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @SuppressLint("CheckResult")
            @Override
            public void afterTextChanged(Editable name) {
                Observable.fromIterable(deliveryEntityList)
                        .filter(deliveryEntity -> deliveryEntity.getExpressDeliveryName().contains(name)).toList()
                        .subscribe(data -> mAdapter.setNewInstance(data));
            }
        });
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case ReturnRegisterPresenter.GET_EXPRESS_DELIVERY_MSG:
                deliveryEntityList = (List<ExpressDeliveryEntity>) message.obj;
                mAdapter.setNewInstance(deliveryEntityList);
                break;
        }
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ExpressDeliveryEntity item) {
        helper.setText(R.id.tv_express_delivery_name, item.getExpressDeliveryName());
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<ExpressDeliveryEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        ReturnRegisterActivity.jumpToNext(this, mAdapter.getItem(position));
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
